const getFileUrl = require('../../service/file.js').getFileDownLoad;//process

/***
 * @desc 检查是否拒绝授权
 * 
 */
function checkAuth() {
  return new Promise((resolve, reject) => {
    wx.authorize({
      scope: 'scope.writePhotosAlbum',
      success: function (res) {
        resolve();
      },
      fail: function (res) {
        //如果拒绝过授权
        wx.openSetting({
          success: (res) => {
            res.authSetting = {
              "scope.writePhotosAlbum": true
            }
          }
        });
        reject();
      },
      complete: function (res) { },
    })
  })
}


function getDownload(fileId) {
  return new Promise((resolve, reject) => {
    getFileUrl(fileId).then(res => {
      // res.DownloadUrl
      // previewImage([res.DownloadUrl])
      wx.downloadFile({
        url: res.DownloadUrl,
        success: function (res) {
          // res.tempFilePath
          if (res.statusCode === 200) {
            resolve(res.tempFilePath);

            // image.saveImage(res.tempFilePath)
            //   .then(res => {
            //     wx.showToast({
            //       title: '保存成功',
            //       icon: 'success'
            //     })
            //   }, () => {
            //     wx.showToast({
            //       title: '保存失败'
            //     })
            //   })

          } else {
            reject();
          }
        }
      })
    }, (err) => {

    })
  })
}

/***
 * @desc 保存到相册
 * @param {saveImage:function}
 * @param （fileId,fileType[文件类型：1图片，2视频]）
 */

function saveImage(fileId, fileType) {

  if(fileType>2){
    wx.showToast({
      title: '不支持保存',
      icon:'loading'
    })
    return;
  }
  return new Promise((resolve, reject) => {
    checkAuth()
      .then(res => {
        getDownload(fileId)
          .then(tempFile => {
            let showSuccess = () => {
              wx.showToast({
                title: '保存成功',
                icon: 'success'
              })
            };
            let showFail = () => {
              wx.showToast({
                title: '保存失败',
                icon: 'loading'
              })
            }

            switch (fileType) {
              case 1:
                wx.saveImageToPhotosAlbum({
                  filePath: tempFile,
                  success: function (res) {
                    showSuccess();
                  },
                  fail: function (res) {
                    console.error('save fail', res)
                    showFail();
                  }
                })
                break;
              case 2:
                wx.saveVideoToPhotosAlbum({
                  filePath: tempFile,
                  success: function (res) {
                    showSuccess();
                  },
                  fail: function (res) {
                    console.error('save fail',res)
                    showFail();
                  },
                })
                break;
              default:
                wx.showToast({
                  title: '不支持保存'
                })
                return;
            };
          }, () => {
            wx.showToast({
              title: '获取下载地址失败'
            })
          })
      }, () => {
        wx.showToast({
          title: '需要授权才能保存'
        })
      })
  })
}

module.exports = saveImage